Yes you can wear shorts, as mentioned there's no dress code for Morocco specifically. You're more likely to find shorts being worn in coastal areas. Really short shorts are not advisable and to be honest, wearing loose fitting, longer pants is often much cooler than wearing shorts where a lot of skin is exposed.