I wanted to update this question because the answers are outdated, and all of this information has been revealed in Chapter 89.
Please note, spoilers ahead, read at your own risk.
In Chapter 89, pages 8-18, we see a summary of Ymir's life in chronological order.
Ymir was originally an Eldian begger in the Marleyan internment zone for Eldians. As a child, she was caught associating herself with Ymir Fritz cultists, and she took the punishment for the cultists. As a result, she was sentenced to life as a mindless titan, taken to the penal colony Island Paradis, injected with titan serum in her spinal fluid, and shoved off the wall into the territory. This results in her roaming the titan territory of the penal colony for 60 years as a mindless titan.
At some point Ymir
hibernates in the earth under dirt and brush until 845 when Reiner, Marcel, Bertholdt, and Annie accidentally stumble upon her in hibernation. Marcel, seeking to save Reiner, sacrifices himself when Ymir tries to eat Reiner. Since Marcel was a titan-shifter he passed on his powers to Ymir, and Ymir regained the ability to shift back to human form. So, in essence, Ymir was only able to return to her human form because she ate a titan-shifter and then became one. All of this was entirely by accident and never meant to occur.