Yes you *could* do this in three weeks. You can cross Spain in 1.5 days. Algeciras to Marrakech on the motorways another day. Marrakech to St Louis four days. Same back again, but that only leaves five spare days.
Riding through Western Sahara is a real bore—an extract from a road report to Dakar that I wrote many years ago...
"the road south of Layoune runs straight into the distance. A line of pylons marches across the hamada alongside the road. After twenty miles there is some excitement as I see a sign for a bend in the distance, but it is a very gentle curve that could have been taken at 200 mph. Five miles further on I overtake a lorry. Ten miles further on two donkeys are standing by the side of the road. Do I sound bored?"
The photos have long gone but you might be interested in the words, see
https://timcullis.wordpress.com/page/2/
And there's some photos at
http://www.morocco-knowledgebase.net/forum/showthread.php?t=191
However my advice would be that your time would be better spent in Morocco. Over the last five decades I've spent almost four years in Morocco and there's still lots more to see and do.