It totally depends on you. If you are confident what you are going to write and its outcome, you can write the title before composing the paper. However, minor refinements are possible at any stage. Even a title can change during the review process. Reviewers might suggest better alternative title.
From my humble experience, I have been written an initial or primary title , then after reaching the final results and conclusion, primary title can be modifying as needed.
I thought about possible informative titles (up to 6 alternatives) during the experimental work. like preliminary results, preliminary titles are also welcome.
Finally, I read the manuscript with all titles and decide.
Thus, I started (before writing) by TITLES to finalize the Title (after writing).
Write a preliminary/working title and then check the requirements of the journal you are submitting to. Some journals ask for titles that have a certain number of words/characters and others ask for specific formats.
I agreed with and recommended Essam A. Al-Moraissi's suggestion. I cant start with a 'finalised' title but where possible i start with PICO based structure that highlights the key attributes of my research direction, then i write the paper, the abstract and then get back to adjusting the title from a working draft to something more complete.