As another answer already mentioned, wmctrl is a good choice for this task and wmctrl -a WINDOWTITLE allows one to activate the window whose title contains WINDOWTITLE.
However, in many cases the window title does not contain the name of the program used. For this purpose wmctrl also has the -x option that can be used to focus windows by class name.
You can type wmctrl -lx to print all currently open windows, together with their class name. You can use wmctrl -xa NAME to activate a Window whose class name includes NAME.
For example,I had the issue that I wanted to activate the window of the Evince PDF viewer if it was open, and I couldn't do that from the window name alone as it always only contains the name of the PDF that is viewed. So I used wmctrl -lx and found the line
0x06000007 0 evince.Evince sejmou-desktop 2. User-User Collaborative Filtering
Where evince.Evince is the window class name mentioned above.
Then, I was able to activate the Evince window programmatically wmctrl -xa Evince. Of course this only works reliably as long as Evince is a unique class name among the windows that are currently open.