So, in short, the perfect squares arise as the sums of the first $k$ odd numbers, and the invariant subspaces arrange themselves into energy levels that way because... well, here I get stuck.
To get "unstuck", the following consideration may help:
The key property to consider is the number $N$ of nodes of the electronic wave function. Wave functions with the same number of nodes have approximately the same energy. We say that states with the same $N$ form a "shell". (The integer $n=N+1$ is called the principal quantum number.)
The number of states ("orbitals") in a shell now follows by counting the number $\sum_{l=0}^{N}(2l+1)= (N+1)^2$ of eigenfunctions of the angular momentum operator with at most $N$ nodes – "at most" because the radial wave function can provide the remaining nodes. Including spin the number of states in a shell is then $2n^2$.
So the $2n^2$ rule applies to shells, labeled by the principal quantum number $n$. The statement that "states with the same $n$ have the same energy" is only an approximation, which is why the rows of the periodic table do not strictly follow the $2n^2$ rule. For example, the $n=3$ row has only 8 elements, not 18, because the $n=4,l=0$ state has lower energy than the $n=3,l=2$ states.
More accurate considerations, see Theoretical justification of Madelung's rule, show that the energy is an increasing function of the number
$$W=n+l-\frac{l}{l+1}.$$
The physics here is that the $n$-dependence of the energy accounts for the attraction of electrons to the core, while the $l$-dependence accounts for their mutual repulsion. In atomic hydrogen, which has a single electron, the energy is only dependent on $n$, without any $l$-dependence (at least if we neglect relativistic effects).
If we approximate $W\approx n+l$ (the socalled "$n+l$ rule") we obtain the length $L_n$ of the $n$-th row in the periodic table as
$$L_n=2\sum_{l=0}^{\text{Int}\,[n/2]}(2l+1)=2\left(1+\text{Int}\,[n/2]\right)^2$$
$$\qquad=2, 8, 8, 18, 18, 32,32,\;\;\text{for}\;\;n=1,2\ldots 7.$$
So this explains why the initial "2" appears only once and the subsequent numbers appear twice (Dan Romik's question).
Bottom line:
Q: "Is there a good mathematical explanation for why orbital lengths in the periodic table are perfect squares doubled?"
A: I don't think so, Madelung's $n+l$ rule requires explicit consideration of the electrostatics of the problem.
However
However, there have been symmetry based attempts to obtain that rule, as described in Ordinal explanation of the periodic system of chemical elements. and in Some evidence about the dynamical group SO(4,2) symmetries of the periodic table of elements.