"By the way" in formal writing

No, I wouldn't recommend doing so, as the phrase is informal. There are many formal alternatives you could use to make you sound more professional, some of those are:

it is worth noting
it might also be noted/observed


The phrase by the way is not especially informal, and you may freely use it in formal situations. However, if you wish to use a variant which is more formal, then you could use a substitute such as:

  • Speaking of which,
  • This brings to mind
  • Apropos

Or one of the phrases mentioned in Rimmer's answer.