why some subs stay under water for months at a time?
My longest stint submerged was 89 days.
Subs have several means to replenish/recycle the air we would breathe.
Oxygen can be replenished in 4 ways.
Ventilate- Out with the old and in with the new, requires a trip to periscope depth and extending a snorkel mast. Some what noisy evolution and risks detection.
Oxygen banks- Oxygen stored at very high pressure that can be slowly released into the sub.
Oxygen generator- Extract O2 from seawater by electrolysis and discharge the hydrogen overboard
Chlorate Candles- Chemical generation of O2. My first boat did not have a Oxygen generator, we burned candles or used the O2 banks.
CO2 removal
CO2 scrubber- removes excess CO2 from atmosphere
https://apps.dtic.mil/sti/citations/ADA389253 and as mentioned in the link, lithium hydroxide which can be placed in a machine and the air pumped through it or in an emergency situation it can even be spread out on the deck to absorb CO2 passively.
CO and hydrogen- they are catalyzed out of the air and burned off with the residue discharged overboard
I've seen O2 levels allowed to get low enough that a Zippo lighter would barely burn and a cigarette (old school) would go out on it's own. Imagine a really bad hangover grade headache with out the hangover stomach issues.
We would routinely take a pressure in the boat just prior to diving as just a matter of course. When surfacing we would have to equalize pressure in the boat before opening hatches or the poor schlub opening it could actually get sort of popped out like a cork.
We did have to ability to pressurize a breached compartment against sea pressure but that was a last resort action.