I want to switch an ESP32 XIAO, which requires approximately 400mA in normal power WiFi mode.
With the 2N2222 transistor, the ESP32 isn't working, but with the MOSFET, everything works as expected. According to the datasheet of the 2N2222, it can handle IC: 800mA, so this should be within the range required.
What am I missing here that prevents me from powering/switching the ESP with the transistor?
Absolute maximum Ic on a 2N2222A is 600mA. You probably shouldn't expect to use that since it requires maintaining the junction (internal) at 25C.
At 500mA current, the beta is 40, which means that you need a base current of 500/40 (12.5mA) to turn it on completely.
I can't read those resistor values very well (draw schematics, not those annoying Fritzing things!!!) but it looks like 220 ohms. Which means that the transistor base current is (3.7 - 0.6)/220 = 14mA at best, but you should be able to get at least the 500mA out.
Note that at 500mA Ic, the saturation voltage is 1V, so you will lose 1V of your 3.7V battery voltage to the transistor. All of this says that you should not try to use a 2222 for this application.
The general rule for a BJT switching transistor is that the base current needs to be at least 10% of the load current for solid on-switching. Note the current gain of the transistor in active mode (aka beta or Hfe) is not relevant to the saturated state in a switch.
The ESP microcontroller probably cannot source the 40mA needed to drive a BJT here, so a MOSFET is the better choice.
There are some better performing BJT switch transistors, like the infamous ZTX951, which need far less base current, perhaps 2 to 3% of the load, in order to switch. The 2N2222 is a dinosaur these days.