Trace of a commutator is zero - but what about the commutator of $x$ and $p$?

$x$ and $p$ do not have finite-dimensional representations. In particular, $xp$ and $px$ are not "trace-class". Loosely, this means that the traces of $xp$ and $px$ are both infinite, although it's best to take them both to be undefined. Again loosely, if you subtract $\infty-\infty$, you can certainly get $i\hbar$. But you shouldn't. Everything works out if you think of $p$ as a complex multiple of the derivative operator, for which $\frac{\partial}{\partial x}$ and $x$ act on the infinite dimensional space of polynomials in $x$.


After reading @Peter Morgan's answer, and giving it some more thought, I think this is actually simpler than it seems at first.

For finite-dimensional spaces the trace of a commutator is indeed always zero. For infinite-dimensional spaces the trace is not always defined, since it takes the form of an infinite sum (for countable dimension) or an integral (for continuous dimension) which do not always converge.
When the trace is defined, it obeys the same rules as in finite dimension, specifically the trace of a commutator is zero. For operators such as $x$, $p$ and their products, the trace is simply not defined, so there is no sense in asking questions about it.
When computing thermal averages, the factor $e^{-\beta H}$ makes sure the trace converges, since the energy is always bound from below (otherwise the system is unphysical).

I'm sure the concepts mentioned by @Peter Morgan are important in this context (boundedness, KMS-condition), but I don't know anything about them, and I think the answer I just provided suffices for practical purposes.


Fleshing out @Peter Morgan's answer a bit, to the effect that $x$ and $p$ are not bounded operators, so their commutator need not be bounded. First note that $$[x^n,p] = i\hbar nx^{n-1} ~,$$ hence the operator norms of both sides satisfy $$ 2\| p\| ~ \|x\|^n \geq \|x^np \| + \|px^n \| \geq n \hbar \|x\|^{n-1}$$ so that, for any $n$, $$2\|x\|~\|p\|\geq n \hbar~. $$ Since $n$ can be arbitrarily large, at least one l.h.s. operator cannot be bounded, and the dimension of the underlying Hilbert space cannot be finite. Utilizing the (bounded) Weyl relations, it can actually be shown that both operators are unbounded.


Now for the magic of the paradox resolving itself:

In finite (N-dimensional) Hilbert space representations of $x$ and $p$ (Weyl's QM around a circle), the commutator does indeed vanish, as it should, but the right hand side is not quite the identity, but a finite matrix with 0s in the diagonal, so, then traceless, all right. Santhanam, T. S.; Tekumalla, A. R. (1976). "Quantum mechanics in finite dimensions". Foundations of Physics 6 (5) p. 583. doi:10.1007/BF00715110.

In this remarkably insightful paper, it is shown that, in the continuum limit, N⟶ ∞, this very matrix goes to the Dirac-δ, the infinite-dimensional identity under discussion here! Cf. Q10,11 of an Exam I've given in the past.