that's weird; that must be a bad joint. It shouldn't've popped off if welded correctly. You can't just push it back on, it will pop off again. You need to glue (solvent weld) it back together.
Or alternatively you can cut the two pipes and replace them with water heater flexes. This would be quickest and easiest and it also solves the problem of having the CPVC connected directly to the water heater.
You'd chop both pipes at a good location and push these on, and then just connect them to the water heater.
Though it looks like the CPVC has a male adapter and the water heater has female. You could use nipples or instead you could use a hose like this:
with adapters like this:
So the male end of the hose screws into the female sockets of the water heater and the female end of the hose goes on the male adapter which connects to the pipes.