Does an expanding universe cool down? I want to know if and why the temperature of an expanding universe decreases with time.

Universe setup. My universe is modeled as the manifold $]0,\infty[\times\mathbb T^3$, where $$\mathbb T^3 = (\mathbb R/\mathbb Z)^3\simeq[0,1[^3$$ is the canonical $3$-torus, describing space, and $]0,\infty[$ is the time ($0$ marking the big bang). The coordinates are cartesian $(t,x,y,z)$.
This manifold shall be equipped with a pseudo-Riemannian metric of the flat Friedmann type (in cartesian coordinates):
$$g = c^2 \,\mathrm dt^2 - a(t)^2 (\mathrm d x^2+\mathrm dy^2+\mathrm dz^2).$$
I will allow ourselves to pick the scale factor $a:]0,\infty[\to]0,\infty[$ to be any smooth function (i.e. we ignore that the Friedmann equation may be violated).
Please also ignore any quantum-mechanical effects in the following.

The universe shall be filled with a, say, ideal gas, consisting of $n\in\mathbb N$ point particles with mass $m\in]0,\infty[$.

Question. Does the temperature of the universe decrease if the scale factor $a$ increases?

My thoughts.

*

*In the rest frame of the particles (which, if I understood correctly, is the frame in which the sum of all velocity vectors of the particles add up to $0$ ?), the temperature is a constant times the average kinetic energy of the particles: $$\frac{3 k_B}2 T = \frac m2 \sqrt{\frac{\lvert v_1\rvert^2+\dots+\lvert v_n\rvert^2}{n}},$$ where $v_i$ is the velocity vector of the $i$-th particle, $T$ is the temperature and $k_B$ is the Boltzmann constant.
This kinetic energy doesn't seem to decrease with time...

*There is an obvious loophole in this, namely that I would have to read a bunch of 40-page publications in the Annals of Physics from 40 years ago in order to do proper relativistic thermodynamics, which I can't do.

*(In fact, $v_i$ is non-relativistically used above, so it isn't actually properly defined in the setting that we are in.)

*Also, in a Joule expansion, the temperature stays constant.

So then I would think that the temperature doesn't change.
But obviously something is totally wrong here, no? I've heard plenty of times that the universe cools as it expands and that shortly after the big bang the universe was very hot.
So, what am I missing? Is the temperature indeed constant in my model above? Or am I wrongly applying thermodynamics?
 A: An expanding universe with energy conservation must cool, because an expanding universe does work on the gravitational field.
I'm hesitant to critique because I don't follow the first part of your formalism, but I'm pretty sure I can answer adequately anyway on the basis of what I can understand.
The problem with your approach:
Your argument takes its conclusion as a premise when it models the universe as a gas with a particle rest frame.
The average velocity of all the particles equaling zero gets us the rest frame of "the particles" in a system for which any subdivision of the particles agrees on the average velocity of every other subdivision of the particles. In a non-expanding universe full of a uniform ideal gas, this is a safe guess. In an expanding universe this is false, since any sufficiently distant segment of the universe will have an average velocity in the direction away from our chosen rest frame.
We also get a nonsense result if we do try to use your temperature formula on a universe with a size and expansion coefficient similar to our own. Measured from any given point, most $v_n > c$.
By choosing a universe for which you can find a particle rest frame, you've chosen a non-expanding and hence non-cooling universe from the start.
Suppose we did start with such a non-expanding universe filled with a uniform ideal gas, and allowed it to expand. Even then it would still cool, because by getting farther apart, each molecule would do work on every other molecule's gravitational field. Expansion that does work against an inward-pointing force isn't Joule expansion, it's adiabatic expansion.
Alternate approaches:
The universe as a cloud of particles doing work on the gravitational field:
In the laboratory, an adiabatic expansion results in cooling, on the microscopic scale, because the particles do positive work on the force that acts against free expansion - that is, the walls of the container.
In an expanding universe, there are no walls, but gravity provides an inward force that opposes free expansion. The particles do work on the gravitational field by getting farther apart.
As far as I know experts are divided on the subject of where (if anywhere) a massless particle's energy goes when it redshifts because of cosmic expansion. Wherever (if anywhere) it goes, though, it's a fact that the photons do cool off at a rate proportionate to the expansion of the universe.
The universe as a space of fields:
The universe expanding is an expansion of a space full of fields. Expanding the space means proportionately decreasing the energy density of the fields.
A: A homogeneously expanding gas cools even if there is nothing for it to do work against. You can see this behavior even in a Newtonian, gravityless, ideal-gas toy model where every particle moves with a constant velocity throughout the experiment.
The reason is that temperature depends not on kinetic energy relative to a fixed global inertial frame, but relative to the local average velocity. Particles that have a large velocity relative to the average in a region don't stay in that region. If the gas is expanding, their peculiar motion takes them to a region where the local average velocity is closer to their own velocity; in the long term, all particles "sort themselves". If the gas is contracting then the opposite happens, and the temperature increases everywhere.
The usual setup in thermodynamics, where you start with a gas confined to a fixed-size box, then expand the box or allow the walls to be pushed outward, is much less homogeneous than what happens in cosmology. In cosmology there is no global frame or global constant temperature, though the local temperature everywhere is the same.
A: Fast expansion: Particles "sort" themselves. Local temperatures decrease.
Slow expansion: Same as fast expansion, but some amount of "unsorting" happens. Local temperatures decrease, but less than in the fast expansion case.
Expansion that stops at some point, if the model allows that kind of thing: First local temperatures decrease, then then local temperatures start increasing. if there was some dark energy involved, then very high local temperatures may be reached.
