Imaginary friends are quite common among children, and there are processes in some mental wellness practices that invoke imaginary friends.
One of them is the wise mind in Dialectic Behavior Therapy, in which one taps into their adulting conscience (related to the adult in transactional analysis).
If a patient struggles directly invoking the wise mind, they can invoke a fiction, similar to the Christian tradition of WWJD A patient struggling with a home management problem might imagine asking Albert Einstein for advice, and then imagine how Einstein might respond. (Substitute anyone, including darker archetypes: Satan, Darth Vader, Joan Collins, Barbara Bush...)
Given some people who do believe in spiritual or supernatural elements might get the same effect from talking to God, or channeling spirits, they can get the same benefit even if their beliefs can be inconsistent either with modern science, or with their own ministries (who want their parishioners to go to them for direction).
So, no, regardless of whether or not delusions, misinformation or self-deceptions are involved, imaginary friends are not intrinsically dysfunctional or a sign of mental illness.