We consider the primitive equations, a fundamental model for oceanic and atmospheric dynamics. This system can be derived from the three-dimensional Navier--Stokes equations with anisotropic viscosities on a thin layer through the hydrostatic approximation procedure. In contrast to the three-dimensional Navier--Stokes equations, global well-posedness results for large initial data in $H^1$ and, more generally, in $H^{2/p, p}$ are known within the horizontally periodic setting. Motivated by the scaling structure, we aim to establish global well-posedness of the primitive equations in an infinite layer for scaling critical initial data. Specifically, for $2<p<\infty$, we assume that the vertical average of the initial data belongs to the homogeneous Besov space $\dot{B}^{-1+2/p}_{p, \infty}(\mathbb{R}^2)$ and its high- and low-frequency tails vanish. The vertically mean-free part of the initial data is assumed to belong to suitable anisotropic scaling critical spaces. The key idea of the proof is to reformulate the original problem as a coupled system consisting of the two-dimensional Navier--Stokes equations and a three-dimensional nonlinear heat equation. In view of the ill-posedness results for the Navier--Stokes equations, the corresponding well-posedness assertion fails in the limiting case $p=\infty$.