Proton Exchange Membrane (PEM) electrolysis is a highly effective method for generating green hydrogen from renewable energy sources. However, effective thermal control is essential for maintaining high efficiency and prolonging the stack lifespan, especially under variable power conditions. This paper develops a four-state thermal model of the entire PEM electrolysis system, including key Balance of Plant (BoP) components such as heat exchangers and separators, to capture the system’s primary temperature dynamics. Building on this model, we propose a two-loop control architecture: a slower PID loop regulates the inlet temperature, while a faster sliding-mode loop maintains a target temperature difference between the inlet and outlet temperatures. We use Particle Swarm Optimization (PSO) to determine the controller parameters, enabling robust operation under changing load conditions. Simulation results demonstrate that the proposed approach not only stabilizes the overall temperature but also mitigates abrupt disturbances from renewable energy sources, thereby improving both the efficiency and durability of the PEM electrolyzer.