Simply speaking, yes, you can arrive at Merzouga by public transport. But note that, you can take a bus from either Marrakech or Fez. In the case of Marrakech, the departure starts daily at 8:30 and the first of those buses arrive at Merzouga at around 21:00.
On the other hand, if you are coming from Fez there are some other things to consider. First of all, you are supposed to be here on a night bus that starts at 20:30 from Fez and reaches Merzouga at around 7:15 the next morning. Early in the morning there are two possible options in front of you. Firstly you can opt for an early check-in which would cost you 50 EURO per person (MINIMUM TWO PERSONS). This would include a tour across the dunes and a lunch in a restaurant.
The alternative option is to stop at the city Rissaini, Roam around there, do some sightseeing & have your lunch there. After lunch, you can take a taxi or something along that line to get from Rissaini to Merzouga. An additional plus point of stopping there is that you get to see a city in the desert how it works and operates. You can find out more about the public transport bus on the website for Supra Tours.