It's not possible to connect PEX to a water heater. Extending a pipe 18 in. is the first thing you should do. connect the PEX to the pipe from your water heater.
Let's Get It Fixed!
HomeX virtual experts will assess your issue (for free!) and can resolve simple problems.