Is it safe?

Don’t judge me, I’m asking because I want to make sure it’s safe. Is it safe for babies to sleep with a hat on? I know in the hospital they put my little one in a hat and didn’t say they couldnt sleep with it on but now that he’s older (9 weeks) is it still safe?