At RAK, stay airside—small airport with limited buffer, so stay near boarding and avoid city transport.
A 3-6 hour layover in Marrakech is one of the best airport-to-city ratios in Africa - Menara Airport is only 6km from the medina. Bus 19 runs to Djemaa el-Fna for 30 MAD ($3, 15 min). Or taxi for 80-100 MAD ($8-10). Spend your time in the square: watch the performers, buy fresh orange juice (4 MAD), eat at the food stalls in the evening. Leave 75 minutes before your flight.
Six to ten hours in Marrakech is excellent. From Djemaa el-Fna, walk into the souk district: the Souk Semmarine is the main entry point into the labyrinth. The Madrasa Ben Youssef (14th century Islamic school, 20 min walk from the square) has the finest tilework and carved stucco in Morocco - arrive early to avoid crowds. The Saadian Tombs (10 min from the square) are small but ornate. Return to the square for lunch: any stall numbered by the municipality is clean. Tagine, couscous, or merguez sandwiches. For the afternoon, a riad rooftop cafe for mint tea is essential. Return by taxi 90 minutes before your flight.
With 10+ hours, go deeper into the medina and beyond. The Jardin Majorelle, bought and restored by Yves Saint Laurent, is one of the great garden designs in Africa - vivid cobalt blue buildings, cacti, and fountains ($15 entry, worth booking ahead). The Museum of Yves Saint Laurent next door documents his Marrakech connection. In the evening, the atmosphere in Djemaa el-Fna shifts dramatically as dozens of food stalls set up and the storytellers, musicians, and acrobats multiply. Eat at the stalls: harira soup, kefta tagine, and brochettes. Palais Bahia (19th century palace, free entry) is a 15-minute walk from the square. Day trip to the Atlas Mountains foothills (Ourika Valley, 1 hour by taxi) for dramatic scenery if you have a full day.