I love these questions that bring back memories. :)
My son did the same thing at the same age. He’d shove at least one hand down the front of his diaper and end up with soggy pjs and soiled sheets every night. He sometimes did this during the day, but only if he was tired and zoning out on the couch with a book or a movie. Anyway, the only way I got him to stop was by dressing him in one-piece pajamas at night. If I hadn’t kept up with laundry and had him in pjs with a separate top and bottom, he’d stick his hands right down there when he went to bed. I have some great blackmail photos for when he’s older. This went on for a few months, and then stopped. He had no rash, infection, or other complaint. I think it was sort of like how some kids twirl their hair or suck their thumbs when they’re sleepy.
If it’s too hot where you live for long pjs, maybe you can find a onesie in his size. Snap it at the crotch, put a soft pair of shorts over it, and see if it gets him to put his hands elsewhere. I don’t think it’s anything to worry about, but I know what a pain it is to change wet sheets in the middle of the night and deal with extra laundry. During the day, remind him to keep his hands out of his pants and have him go wash them whenever you see him do it. Don’t make a big deal out of it or humiliate him, just practice good hygiene.
Oh, and a side note on the onesies: If you are starting to potty-train and worry that a onesie will get in the way, just unsnap it at the bottom, pull the snaps up over his tummy, and resnap them over one shoulder like a toga. It keeps the onesie out of the way while he practices using the toilet. He’ll just need your help to resnap before he pulls his pants up.