Good question. This is often a problem that occurs in intermediate games of opposite side castling. An experienced player wouldn't even consider a move like h5. Why is that? Simply because you're weakening the g5 square irreparably and effectively creating a possible hook for your opponent to exploit. As a general rule of thumb, don't push pawns in front of your king unless you have to. Better would be to expand on the queenside with a move like a5 followed by b5 and start creating weaknesses on the white king. If you notice, white has essentially no pieces defending the king on the queenside. While for you, you have many defenders on the kingside making a kingside attack for white less possible. Your pieces are also better placed to spring into action with moves like Nd5, Qa5, Qb6, Bb4, whereas white's pieces are all tripping over each other with little coordination.
White's plan would be to play h5 followed by h6, or if black responds h6 then follow up with g2-g4-g5. Only one problem, how could white possibly achieve that when the knight is on g3 and moving it would surrender central control of e4, allowing black's knights to get active? The only way for white to achieve this would be something like Qd3 followed by Ne4 to trade a pair of knights and free up the g pawn to be moved. This is a lengthy plan which allows you to stir up more than enough counterplay on the queenside in the mean time. By playing h5 yourself, you've effectively solved his problem of inactive and uncoordinated pieces by allowing white to obtain the g5 square for free and consequently the f4 square by allowing Nf4 with tempo on h5 (which if you are to defend with g6, only creates more weak squares around your king).
After a5, h5 by white isn't a threat since you can meet it with h6, stopping whites pawn in its tracks and with the knight on g3, the g pawn will be extremely tough to get moving for white which allows you plenty of time to play on the queen side in the mean time. As mentioned before, white's plan would be to remove the knight from g3, without allowing black's knight to hop in the center and gain activity, followed by g2-g4-g5. This plan being way too slow and effectively impossible.
Allow me to show this on a board for clarity.
I always end up in these middlegame situations where I don’t know how to continue. This time I played h5 as black to stop his pawn from advancing, but it turned out to be a mistake. I don’t understand why, and I don’t know what I should have been thinking instead.
Sure, I understand I’m weakening my king, but is that really a threat here? Can’t I just expand on the queenside or something?
Can someone explain?