Yes, you can buy tickets for Morocco Trains online. Even more, using Rail Ninja, you can get your tickets for up to 2 years before the planned trip!